package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.Reserve;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.ReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api("专家预约相关模块")
@RequestMapping("/reserve")
public class ReserveController {

    @Autowired
    private ReserveService reserveService;
    /**
     * @Description : 添加预约
     *  * @param reserve
     * @Return : com.qst.crop.common.Result
     */
    @PostMapping("/add")
    @ApiOperation("添加预约")
    public Result add(@RequestBody Reserve reserve) {
        log.info("添加预约");
        reserveService.add(reserve);
        return new Result<>(true, StatusCode.OK,"添加成功");
    }


    /**
     * 查询当前用户的reserve
     *      questioner
     *      expert
     * @param role
     * @return
     */
    @GetMapping("/selectByKind/{role}")
    public Result<List<Reserve>> selectByKind(@PathVariable("role")String role){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        Result<List<Reserve>> rst = null;
        if(role.equals("questioner")){
            rst = new Result<>(true, StatusCode.OK,"查询成功",reserveService.selectByKindQuestioner(username));
        }else if(role.equals("expert")){
            rst = new Result<>(true, StatusCode.OK,"查询成功",reserveService.selectByKindExport(username));
        }

        return rst;
    }


    /**
     * 根据id查询reserve
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result<Reserve> selectById(@PathVariable("id") Integer id){
        return new Result<>(true, StatusCode.OK,"查询成功",reserveService.selectById(id));
    }

    /**
     * 修改reserve通过id
     * @param reserve
     * @return
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody Reserve reserve){
        return new Result<>(true, StatusCode.OK,"修改成功", reserveService.update(reserve));
    }

    @DeleteMapping("/delete/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id){
        return new Result<>(true, StatusCode.OK,"删除成功", reserveService.delById(id));
    }
}
